package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.QuestionContentActivity;
import com.ihighest.app.infox.tools.LongTimeForData;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class NewListViewAdapter extends BaseAdapter {

	private final LayoutInflater inflater;
	Context context;
	JSONArray jsonArray;
	JSONArray images;

	public NewListViewAdapter(Context context, JSONArray jsonArray) {
		super();

		this.context = context;
		this.jsonArray = jsonArray;
		inflater = LayoutInflater.from(context);
		images = new JSONArray();

	}

	public NewListViewAdapter(Context context) {
		super();
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {

		if (jsonArray == null) {
			return 0;
		}
		return jsonArray.length();
	}

	@Override
	public Object getItem(int arg0) {
		try {
			return jsonArray.getJSONObject(arg0);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int posion, View convertView, ViewGroup arg2) {

		ViewHolder holder;
		if (convertView == null) {

			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_nj_new_itemlayout, null);
			holder.new_item_icon = (ImageView) convertView.findViewById(R.id.id_new_item_icon);
			holder.new_item_name = (TextView) convertView.findViewById(R.id.id_new_item_name);
			holder.new_item_city = (TextView) convertView.findViewById(R.id.id_new_item_city);
			holder.new_item_time = (TextView) convertView.findViewById(R.id.id_new_item_time);
			holder.new_item_type = (TextView) convertView.findViewById(R.id.id_new_item_type);
			holder.new_item_title = (TextView) convertView.findViewById(R.id.id_new_item_title);
			holder.new_item_img = (ImageView) convertView.findViewById(R.id.id_new_item_img);
			holder.new_item_resolve = (TextView) convertView.findViewById(R.id.id_new_item_resolve);
			holder.new_item_howresolve = (TextView) convertView.findViewById(R.id.id_new_item_howresolve);
			holder.new_item_whoresolve = (TextView) convertView.findViewById(R.id.id_new_item_whoresolve);
			holder.new_item_resolvemumber = (TextView) convertView.findViewById(R.id.id_new_item_resolvemumber);
			holder.new_item_answerimg = (ImageView) convertView.findViewById(R.id.id_new_item_answerimg);
			holder.new_item_answertv = (TextView) convertView.findViewById(R.id.id_new_item_answertv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			final String id = jsonArray.getJSONObject(posion).getString("id");

			String name = jsonArray.getJSONObject(posion).getString("mname");
			if (name.length() >= 6) {
				holder.new_item_name.setText(name.substring(0, 6) + "...");
			} else {

				holder.new_item_name.setText(name);
			}
			holder.new_item_time.setText(LongTimeForData.transferLongToDate(jsonArray.getJSONObject(posion).getLong("time")));
			holder.new_item_title.setText(jsonArray.getJSONObject(posion).getString("content"));
			holder.new_item_type.setText(jsonArray.getJSONObject(posion).getString("tag"));
			String city = jsonArray.getJSONObject(posion).getString("area");

//			int end1 = city.indexOf('市');
//			
//			int end = city.indexOf('县');
//
//			if (end1 <= end) {
//				end = end1;
//			}
//			city = city.substring(4, end);
			if(city.length()>5){
				city=city.substring(0, 5);
			}

			holder.new_item_city.setText(city);
			holder.new_item_resolvemumber.setText(jsonArray.getJSONObject(posion).getInt("answers") + "人回答");
			String iconurl = jsonArray.getJSONObject(posion).getString("image");
			images = jsonArray.getJSONObject(posion).getJSONArray("images");
			String state = jsonArray.getJSONObject(posion).getString("state");
			String gold = jsonArray.getJSONObject(posion).getString("gold");
//			DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_person_no_icon)
//					.showImageOnFail(R.drawable.infox_person_no_icon).showImageOnLoading(R.drawable.infox_person_no_icon)
//					.cacheInMemory(true).cacheOnDisc(true).build();
//			ImageLoader.getInstance().displayImage(iconurl, holder.new_item_icon, defaultOptions);
              AppKit.SetUserImg(iconurl,  holder.new_item_icon);
			
			
			if (images.length() > 0) {
				holder.new_item_img.setVisibility(View.VISIBLE);
//				DisplayImageOptions defaultOption = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
//						.showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong)
//						.cacheInMemory(true).cacheOnDisc(true).build();
//				ImageLoader.getInstance().displayImage(images.get(0).toString(), holder.new_item_img, defaultOption);
			
			AppKit.SetNormalImg(images.get(0).toString(), holder.new_item_img);
			} else {
				holder.new_item_img.setVisibility(View.GONE);
			}
			if (state.equals("0")) {// 状态等于未解决

				if (SharedPreferencesKit.getJsonObject(context, Const.SP_KEY_CONFIG_JSON) != null
						&& SharedPreferencesKit.getJsonObject(context, Const.SP_KEY_CONFIG_JSON).has("display_Unsolved")
						&& "false".equalsIgnoreCase(SharedPreferencesKit.getJsonObject(context, Const.SP_KEY_CONFIG_JSON).getString(
								"display_Unsolved"))) {
					holder.new_item_resolve.setVisibility(View.GONE);

				} else {
					holder.new_item_resolve.setVisibility(View.VISIBLE);

				}
				holder.new_item_resolve.setText("未解决");
				holder.new_item_resolve.setTextColor(context.getResources().getColor(R.color.not_resolve_color));
				holder.new_item_resolve.setBackgroundResource(R.drawable.infox_information_hotbg);
				holder.new_item_howresolve.setVisibility(View.VISIBLE);
				holder.new_item_whoresolve.setVisibility(View.GONE);
				holder.new_item_answerimg.setVisibility(View.VISIBLE);
				holder.new_item_answertv.setVisibility(View.VISIBLE);

				if (gold != null && !"0".equals(gold))// 金币悬赏〉0
				{
					holder.new_item_howresolve.setText("悬赏" + gold + "金币");

				} else {
					holder.new_item_howresolve.setVisibility(View.GONE);

				}

			} else {

				holder.new_item_resolve.setVisibility(View.VISIBLE);
				holder.new_item_resolve.setText("已解决");
				holder.new_item_resolve.setTextColor(context.getResources().getColor(R.color.resolve_color));
				holder.new_item_resolve.setBackgroundResource(R.drawable.infox_information_dingyuebg);
				holder.new_item_answerimg.setVisibility(View.GONE);
				holder.new_item_answertv.setVisibility(View.GONE);
				holder.new_item_howresolve.setVisibility(View.VISIBLE);
				holder.new_item_whoresolve.setVisibility(View.VISIBLE);
				String type = jsonArray.getJSONObject(posion).getJSONObject("answer").getString("type");
				if (type.equals("Regular")) {
					type = "会员";
				} else if (type.equals("Specialist")) {
					type = jsonArray.getJSONObject(posion).getJSONObject("answer").getString("rank");

				} else if (type.equals("Enterprise")) {
					type = "企业会员";
				} else if (type.equals("Outlet")) {
					type = "企业店主";
				}

				holder.new_item_howresolve.setText("来自" + type + ": ");
				holder.new_item_whoresolve.setText(jsonArray.getJSONObject(posion).getJSONObject("answer").getString("title"));

			}

			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

					Intent intent = new Intent(context, QuestionContentActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("id", id);
					intent.putExtras(bundle);
					context.startActivity(intent);
				}
			});

		} catch (JSONException e) {
			e.printStackTrace();
		}

		return convertView;
	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {

		

		this.jsonArray = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArra) {

		try {
			jsonArray = AppKit.joinJSONArray(jsonArray, jsonArra);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		notifyDataSetChanged();
	}

	class ViewHolder {

		ImageView new_item_icon;
		TextView new_item_name;
		TextView new_item_city;
		TextView new_item_time;
		TextView new_item_type;
		TextView new_item_title;
		ImageView new_item_img;
		TextView new_item_resolve;
		TextView new_item_howresolve;
		TextView new_item_whoresolve;
		TextView new_item_resolvemumber;
		ImageView new_item_answerimg;
		TextView new_item_answertv;

	}

}
